package moni.anyou.com.view.view.team.adapter;


import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.base.BaseFragment;
import moni.anyou.com.view.bean.response.ResLiveBean;
import moni.anyou.com.view.bean.response.base.ResTeamBean;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.view.LeaderDetailActivity;


/**
 * Created by Administrator on 2016/11/21.
 */

public class TeamRecycleAdapter extends RecyclerView.Adapter<TeamRecycleAdapter.MyViewHold> implements View.OnClickListener {

    private BaseActivity mContext;
    private LayoutInflater mInflater;
    private List<ResTeamBean.TeamBean> mItems=new ArrayList<>();

    public TeamRecycleAdapter(BaseActivity context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
    }
    public TeamRecycleAdapter(LeaderDetailActivity context,List<ResTeamBean.TeamBean> items) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
        mItems = items;
    }


    View mView;

    @Override
    public MyViewHold onCreateViewHolder(ViewGroup parent, int viewType) {

        MyViewHold holder = null;

        if (holder == null) {
            mView = mInflater.inflate(R.layout.activity_leader_detail_item, parent, false);
            holder = new MyViewHold(mView);
            mView.setTag(holder);
        } else {
            holder = (MyViewHold) mView.getTag();
        }
        mView.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHold mViewHold, final int position) {
        mViewHold.itemView.setTag(position);
        final ResTeamBean.TeamBean bean = mItems.get(position);

        mContext.setBitmaptoImageView(SysConfig.PicUrl+bean.teamPic,
                mViewHold.iv_teampic,
                R.drawable.loading_null_21,
                R.drawable.loading_null_21,
                R.drawable.loading_err_21);
        mViewHold.tv_member_num.setText("成员:"+bean.memberNumber+"人");
        mViewHold.tv_teamname.setText(bean.teamName);
        mViewHold.tv_startTime.setText("时间:"+bean.startTime+"到"+bean.endTime);
        mViewHold.tv_routename.setText(bean.routeName);
        mViewHold.tv_teamleader.setText("领队:"+bean.nick);
        if (bean.isjoin.equals("1")) {
            mViewHold.tv_join.setVisibility(View.INVISIBLE);
        }
        else{
            mViewHold.tv_join.setVisibility(View.VISIBLE);
        }
        mViewHold.tv_join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //mContext.marklike(position, getItem(position));
                mOnJoinTeamClickListener.onItemClick(position, bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mItems != null) {
            return mItems.size();
        }
        return 0;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (ResTeamBean.TeamBean) mItems.get((Integer) v.getTag()));
        }
    }

    class MyViewHold extends RecyclerView.ViewHolder {
        ImageView iv_teampic;
        TextView tv_member_num;
        TextView tv_teamname;
        TextView tv_startTime;
        TextView tv_routename;
        TextView tv_teamleader;
        TextView tv_join;
        public MyViewHold(View itemView) {
            super(itemView);
            iv_teampic = (ImageView) itemView.findViewById(R.id.iv_teampic);
            tv_member_num = (TextView) itemView.findViewById(R.id.tv_member_num);
            tv_teamname = (TextView) itemView.findViewById(R.id.tv_teamname);
           // rlteam = (RelativeLayout) itemView.findViewById(R.id.rl_item_team);
            tv_startTime = (TextView) itemView.findViewById(R.id.tv_startTime);
            tv_routename = (TextView) itemView.findViewById(R.id.tv_routename);
            tv_teamleader = (TextView) itemView.findViewById(R.id.tv_teamleader);
            tv_join = (TextView) itemView.findViewById(R.id.tv_join);
        }
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, ResTeamBean.TeamBean data);
    }

    public void setmOnItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private OnJoinTeamClickListener mOnJoinTeamClickListener = null;

    //define interface
    public static interface OnJoinTeamClickListener {
        void onItemClick(int position, ResTeamBean.TeamBean data);
    }

    public void setOnJoinTeamClickListener(OnJoinTeamClickListener mOnJoinTeamClickListener) {
        this.mOnJoinTeamClickListener = mOnJoinTeamClickListener;
    }




    public void addDatas(List<ResTeamBean.TeamBean>result) {

        if (mItems != null && result != null) {
            mItems.addAll(result);
        }
        notifyDataSetChanged();
    }
    public void setDatas(List<ResTeamBean.TeamBean>result) {
        if (result != null && mItems != null) {
            mItems.clear();
        }
        if (result != null) {
            mItems.addAll(result);
        }
        notifyDataSetChanged();
    }

}
